package com.example.myapplication.tack_19;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.SearchEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.myapplication.R;
import com.example.myapplication.data.HomeBanner;
import com.example.myapplication.fragment.HomeFragment;
import com.example.myapplication.tack_19.adapter.ArrangeAdapter;
import com.example.myapplication.tack_19.data.ArrangeData;
import com.example.myapplication.utils.BannerTool;
import com.example.myapplication.utils.OkHttpTool;

import java.util.ArrayList;
import java.util.List;

public class FilmActivity extends AppCompatActivity {
    private ImageView exit;
    private ViewPager banner;
    private RecyclerView arrange;
    private TextView ck;
    private SearchView search;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tack_19_activity_film);
        init();
        exit_();
        getBanner();
        String url = "prod-api/api/movie/film/preview/list?pageSize=5&pageNum=1";
        ck_();
        search_();
        getArrange(url);

    }

    private void search_() {
        search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                QUery(query);
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                NewText(newText);
                return true;
            }
        });
    }

    private void NewText(String newText) {
        if(!"".equals(newText) || !TextUtils.isEmpty(newText)){
            getSearch(newText);
        }else{

        }
    }

    private void QUery(String query) {
        if(!"".equals(query) || !TextUtils.isEmpty(query)){
            getSearch(query);
        }else{

        }
    }
    private void getSearch(String name) {
        String url = "prod-api/api/movie/film/preview/list?name="+name;
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, ArrangeData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                ArrangeData arrange  = (ArrangeData)obj;
                if(arrange.getCode() == 200){
                    List<ArrangeData.RowsDTO> listArrange = null;
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            for (ArrangeData.RowsDTO row : arrange.getRows()) {
                                if (row.getName().toLowerCase().contains(name.trim())){
                                    listArrange.add(row);
                                }
                            }
                            ArrAdapter(listArrange);
                        }
                    });
                }
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    private void ck_() {
        ck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ck.setVisibility(View.GONE);
                String url = "prod-api/api/movie/film/preview/list";
                getArrange(url);
            }
        });
    }

    private void getArrange(String url) {

        OkHttpTool.doNetRequest(url, OkHttpTool.GET, ArrangeData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                ArrangeData arrange  = (ArrangeData)obj;
                if(arrange.getCode() == 200){
                    List<ArrangeData.RowsDTO> listArrange = arrange.getRows();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ArrAdapter(listArrange);
                        }
                    });
                }
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    private void ArrAdapter(List<ArrangeData.RowsDTO> listArrange) {
        ArrangeAdapter arrangeAdapter = new ArrangeAdapter(listArrange, FilmActivity.this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(FilmActivity.this);
        arrange.setLayoutManager(linearLayoutManager);
        arrange.setAdapter(arrangeAdapter);
    }

    private void getBanner() {
        String url = "prod-api/api/movie/rotation/list";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, HomeBanner.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                HomeBanner homeBanner = (HomeBanner)obj;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ArrayList<String> list = new ArrayList<>();
                        for (HomeBanner.RowsDTO rowsDTO :  homeBanner.getRows()) {
                            list.add(rowsDTO.getAdvImg());
                        }
                        BannerTool bannerTool = new BannerTool(FilmActivity.this,list,banner);
                        banner.setAdapter(bannerTool);
                    }
                });

            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }


    private void exit_() {
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FilmActivity.this.finish();
            }
        });
    }

    private void init() {
        exit = findViewById(R.id.exit);
        arrange = findViewById(R.id.arrange);
        banner = findViewById(R.id.banner);
        ck = findViewById(R.id.ck);
        search = findViewById(R.id.sear);

    }
}